﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI.WebControls;
using DMS.Business.Modules.Configuation;
using DMS.Business.Utility;
using DMS.Business.Modules.Organization;

namespace DMS.Web.Common.Controls
{
    public partial class MaterialQuotaSetting : System.Web.UI.UserControl, IConfigSetting
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.InitControl();
            }
        }

        #region IConfigSetting Members

        public string ConfigID
        {
            get
            {
                return this.hidConfigID.Value;
            }
            set
            {
                this.hidConfigID.Value = value;
            }
        }
        
        public string MemberID
        {
            get
            {
                return this.hidMemberID.Value;
            }
            set
            {
                this.hidMemberID.Value = value;
            }
        }

        public string MemberType
        {
            get
            {
                return this.hidMemberType.Value;
            }
            set
            {
                this.hidMemberType.Value = value;
            }
        }

        public void InitForm()
        {
            if (string.IsNullOrEmpty(this.MemberID))
            {
                this.InitSysConfigForm();
            }
            else
            {
                this.InitMemberConfigForm();
            }
        }

        public void Save()
        {
            if (string.IsNullOrEmpty(this.MemberID))
            {
                this.SaveSysConfigForm();
            }
            else
            {
                this.SaveMemberconfigForm();
            }
        }
        #endregion

        #region 界面逻辑
        private void InitControl()
        {
            this.lbMConifgDetail.Attributes["ondblclick"] = "javascript:RemoveItem(this);";
        }
        private void InitSysConfigForm()
        {
            SysConfig config = ConfigHelper.GetSysConfigByID(this.ConfigID);
            if (config != null)
            {
                this.lblConfigTitle.Text = config.Name;
            }
        }
        private void InitMemberConfigForm()
        {
            SysConfig defaultConfig = null;
            List<MemberConfig> memberConfigs = ConfigHelper.GetMemberConfigs(MemberID);
            MemberConfig config = (from c in memberConfigs
                                   where c.ID == this.ConfigID
                                   select c).ToList().DefaultIfEmpty(null).FirstOrDefault();
            if (config != null)
            {
                defaultConfig = config.Default;
                this.lblConfigTitle.Text = config.Name;
                this.SetConfigValue(config.Value);
            }
            else
            {
                defaultConfig = ConfigHelper.GetSysConfigByID(this.ConfigID);
                if (defaultConfig != null)
                {
                this.lblConfigTitle.Text = defaultConfig.Name;
                this.SetConfigValue(defaultConfig.Value);
                }
                else
                {
                    ClientHelper.Instance.Alert("读取默认配置项（部门物资申请数量限额）时出错，请联系管理员。");
                }
            }
        }
        private void SaveSysConfigForm()
        {
        }
        private void SaveMemberconfigForm()
        {

            List<MemberConfig> memberConfigs = ConfigHelper.GetMemberConfigs(MemberID);
            MemberConfig config = (from c in memberConfigs
                                   where c.ID == this.ConfigID
                                   select c).ToList().DefaultIfEmpty(null).FirstOrDefault();
            if (config == null)
            {
                Department dept = UserHelper.GetDepartmentByID(MemberID);
                SysConfig defaultConfig = ConfigHelper.GetSysConfigByID(this.ConfigID);

                config = new MemberConfig(dept, defaultConfig);
            }

            config.Value = this.GetConfigValue();
            config.Save();

            this.InitForm();
        }
        private string GetConfigValue()
        {
            return this.hidSelectedItem.Value;
            //StringBuilder sb = new StringBuilder();
            //foreach (ListItem item in this.lbMConifgDetail.Items)
            //{
            //    sb.AppendFormat("{0};", item.Value);
            //}

            //return sb.ToString();
        }
        private void SetConfigValue(string value)
        {
            //string[] materials = value.Split(";".ToCharArray());
            //foreach (string material in materials)
            //{
            //    if (!material.Contains(":"))
            //    {
            //        continue;
            //    }

            //    string[] infoArray = material.Split(":".ToCharArray());
            //    if (infoArray.Length == 2)
            //    {
            //        Guid mID = new Guid(infoArray[0]);
            //        MaterialInfo m = MaterialHelper.GetMaterialByID(mID);

            //        ListItem item = new ListItem();
            //        item.Text = string.Format("物资：{0}，数量限制：{1}", m.Name, infoArray[1]);
            //        item.Value = string.Format("{0}:{1};", infoArray[0], infoArray[1]);

            //        this.lbMConifgDetail.Items.Insert(0, item);
            //    }
            //}

            //this.hidSelectedItem.Value = value;
        }
        #endregion
    }
}